package cn.jja8.test.testtttt;

import com.github.steveice10.mc.protocol.MinecraftProtocol;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundDisconnectPacket;
import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundLoginDisconnectPacket;
import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
import com.github.steveice10.packetlib.event.session.PacketErrorEvent;
import com.github.steveice10.packetlib.event.session.SessionAdapter;
import com.github.steveice10.packetlib.packet.Packet;
import com.github.steveice10.packetlib.tcp.TcpClientSession;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        List<client> clientList = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        System.out.print("放服务器地址:");
        String host = scanner.next();
        System.out.print("服务器端口:");
        int port = scanner.nextInt();
        int i = 0;
        while (true){
            System.out.print("玩家名前缀:");
            String playerName = scanner.next();
            System.out.print("玩家数量:");
            int n = scanner.nextInt();
            System.out.print("休眠:");
            long time = scanner.nextLong();
            for (int i1 = 0; i1 < n; i1++) {
                i++;
                clientList.add(new client(playerName+i,host,port));
                if (time>0){
                    try {
                        Thread.sleep(time);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }

    static class client{
        private final String playerName;
        private final  String host;
        private final  int port;

        private final SessionAdapter sessionAdapter;
        private Session session;
        public client(String playerName, String host, int port) {
            this.playerName = playerName;
            this.host = host;
            this.port = port;
            this.sessionAdapter = new SessionAdapter(){
                @Override
                public void packetReceived(Session session, Packet packet) {
                    if (packet instanceof ClientboundDisconnectPacket) {
                        ClientboundDisconnectPacket p = (ClientboundDisconnectPacket)packet;
                        System.out.println(p.getReason());
                        session.disconnect("byby");
                    }
                    if (packet instanceof ClientboundLoginDisconnectPacket) {
                        ClientboundLoginDisconnectPacket p = (ClientboundLoginDisconnectPacket)packet;
                        session.disconnect("bay");
                        System.out.println(p.getReason());
                    }
                }

                @Override
                public void packetError(PacketErrorEvent event) {
                    event.getCause().printStackTrace();
                }

                @Override
                public void disconnected(DisconnectedEvent event) {
                    client("重新连接");
                }
            };
            client("连接！");
        }
        public void client(String xiaoxi){
            session = new TcpClientSession(host, port, new MinecraftProtocol(playerName));
            session.addListener(sessionAdapter);
            session.connect();
            System.out.println(playerName+"   "+xiaoxi);
        }
    }
}